<?php
/**
 * Mailing controller
 *
 * @author Valery
 *
 */

class MailinglistController extends modules_default_controllers_ControllerUserarea
{
	public $ajaxable = array(
		'delete' => array('json'),
		'additem' => array('json'),
		'deleteitem' => array('json'),
	);

	protected $_contentLayout = 'contentonly';

	//----------------------------------------------------------------------------------------------------

	public function init()
	{
		$oZendSession = Zend_Registry::getInstance()->get('Zend_Session_Namespace');
		unset($oZendSession->mailingListId);

		if (! $this->getRequest()->isXmlHttpRequest())
		{
			$this->view->title = 'Mailing Lists';
			parent::init();

			$this->view->headLink()->appendStylesheet('/css/jquery.alerts.css');


			$this->view->headScript()->appendFile('/js/jquery.alerts.js');

		}

		$this->_helper->AjaxContext()->initContext('json');
	}

//----------------------------------------------------------------------------------------------------

	public function indexAction()
	{
		$allItems = models_MailinglistMapper::findByUserId(Lemar_User::getLoggedUserId());
		$this->view->allItems = $allItems;
	}

//----------------------------------------------------------------------------------------------------

	public function viewAction()
	{
		$idList = (int)$this->_getParam('id', 0);
		$mailingList = models_MailinglistMapper::findById($idList);

		if (null == $mailingList || $mailingList->idUser != Lemar_User::getLoggedUserId())
		{
			throw new Zend_Controller_Action_Exception('This page dont exist',404);
		}

		$allItems = models_MailinglistMapper::findItemsByListId($idList);
		$this->view->mailingList = $mailingList;
		$this->view->allItems = $allItems;
	}

//----------------------------------------------------------------------------------------------------

	public function editAction()
	{
		$idList = (int)$this->_getParam('id', 0);
		$mailingList = models_MailinglistMapper::findById($idList);

		if (null == $mailingList || $mailingList->idUser != Lemar_User::getLoggedUserId())
		{
			throw new Zend_Controller_Action_Exception('This page dont exist',404);
		}

		$addressItems = models_AddressMapper::findNotInML(Lemar_User::getLoggedUserId(), $idList);
		$this->view->addressItems = $addressItems;
		$listItems = models_MailinglistMapper::findItemsByListId($idList);
		$this->view->listItems = $listItems;
		$this->view->mailingList = $mailingList;
	}

//----------------------------------------------------------------------------------------------------

	public function newAction()
	{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();

		$listName = $this->_getParam('ml_name');
		$idUser = Lemar_User::getLoggedUserId();

		models_MailinglistMapper::save(
			array(
				'user_id' => $idUser,
				'name' => $listName
			)
		);

		$this->_redirect('/mailinglist/');
	}

//----------------------------------------------------------------------------------------------------

	public function selectAction()
	{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();

		$idList = (int)$this->_getParam('id', 0);

		$mailingList = models_MailinglistMapper::findById($idList);
		$idUser = Lemar_User::getLoggedUserId();

		if ($mailingList->idUser == $idUser)
		{
			$oZendSession = Zend_Registry::getInstance()->get('Zend_Session_Namespace');
			$oZendSession->mailingListId = $idList;
		}
		$this->_redirect('/');
	}

//----------------------------------------------------------------------------------------------------

	public function exitAction()
	{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();

		$oZendSession = Zend_Registry::getInstance()->get('Zend_Session_Namespace');
		unset($oZendSession->mailingListId);

		$this->_redirect('/');
	}

//----------------------------------------------------------------------------------------------------

	public function additemAction()
	{
		$idList = (int)$this->_getParam('list_id', 0);
		$items = explode(';',$this->_getParam('items', ''));
		$mailingList = models_MailinglistMapper::findById($idList);

		if (null == $mailingList || $mailingList->idUser != Lemar_User::getLoggedUserId())
		{
			throw new Zend_Controller_Action_Exception('This page dont exist',404);
		}

		foreach ($items as $item)
		{
			$address = models_AddressMapper::findById($item);
			if (Lemar_User::getLoggedUserId() == $address->idUser)
			{
				models_MailinglistMapper::addItemToList($idList, $item);
			}
		}
	}

//----------------------------------------------------------------------------------------------------

	public function deleteitemAction()
	{
		$idList = (int)$this->_getParam('list_id', 0);
		$items = explode(';',$this->_getParam('items', ''));
		$mailingList = models_MailinglistMapper::findById($idList);

		if (null == $mailingList || $mailingList->idUser != Lemar_User::getLoggedUserId())
		{
			throw new Zend_Controller_Action_Exception('This page dont exist',404);
		}

		foreach ($items as $item)
		{
			$address = models_AddressMapper::findById($item);
			if (Lemar_User::getLoggedUserId() == $address->idUser)
			{
				models_MailinglistMapper::deleteItemFromList($idList, $item);
			}
		}
	}

//----------------------------------------------------------------------------------------------------

	public function deleteAction()
	{
		$idList = (int)$this->_getParam('list_id', 0);

		$mailingList = models_MailinglistMapper::findById($idList);

		if ($mailingList->idUser == Lemar_User::getLoggedUserId())
		{
			models_MailinglistMapper::deleteFromBase($idList, 'models_DbTable_Mailinglist');
			$this->view->ret = 1;
		}
		else
		{
			$this->view->ret = 0;
		}

	}

//----------------------------------------------------------------------------------------------------
}